﻿using System;
using System.Windows.Forms;
using ExampleApplication.Models;
using ExampleApplication.SQLProxy;
using SQLPXBase;
using SQLPXBase.Factory;

namespace ExampleApplication
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private sql proxy;
        private SQLPXBase.Factory.ModelFactory<sql> factory;

        private void Main_Load(object sender, EventArgs e)
        {
            textBox1.AppendText("Initializing proxy.." + Environment.NewLine);
            textBox1.AppendText("Creating factory.." + Environment.NewLine);

            factory = new ModelFactory<sql>(new ProxyManager<sql>(new SQLConnectionString(
                                                                      "LOCALHOST\\SQL2008",
                                                                      "test",
                                                                      "sa",
                                                                      "koinelog2004"
                                                                      )));

            textBox1.AppendText("Loading people.." + Environment.NewLine);

            PersonCollection people = factory.List<PersonCollection, Person>(new PersonCollection.List());

            foreach (Person p in people)
            {
                textBox1.AppendText("--------------------------------------------------------" + Environment.NewLine);
                textBox1.AppendText(string.Format("Surname: {0}{1}", p.Surname, Environment.NewLine));
                textBox1.AppendText(string.Format("Name: {0}{1}", p.Name, Environment.NewLine));
                textBox1.AppendText(string.Format("Date of Birth: {0:dd/MM/yyyy}{1}", p.DateOfBirth, Environment.NewLine));

                if (p.CarsOwned != null)
                {
                    textBox1.AppendText(Environment.NewLine);
                    textBox1.AppendText("CARS: " + Environment.NewLine);

                    foreach (Car c in p.CarsOwned)
                        textBox1.AppendText(string.Format("Plate: {0} - {1}{2}", c.PlateNumber, c.Model,
                                                          Environment.NewLine));
                    textBox1.AppendText(Environment.NewLine);
                }
            }
        }
    }
}
